﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Them_SuaXoa_LoaiDT : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    KetNoi obj = new KetNoi();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)// Ko co phan nay ko update dc
        {
           
            LoadGridview();
        }
     

    }
    protected void btnthem_Click(object sender, EventArgs e)
    {
        string maloai = txtmaloai.Text.ToString();
        string tenloai = txttenloai.Text.ToString();
        obj.ThemLoaiDT(maloai, tenloai);
        Response.Write("<script language='javascript'> alert('Bạn đã thêm thành công !' ) </script>");
        LoadGridview();
        XoaText();
    }

    protected void LoadGridview()
    {
        dt = obj.loadDataTable("Select * from Loai");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
  
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadGridview();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
       GridView1.EditIndex = -1;
       LoadGridview();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string maloai = GridView1.DataKeys[e.RowIndex].Value.ToString();
        obj.XoaLoaiDienThoai(maloai);
        Response.Write("<script language='javascript'> alert('Xoá thành công !' ) </script>");
        LoadGridview();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string maloai = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string tenloai = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
        obj.UpdateLoaiDienThoai(tenloai, maloai);
        GridView1.EditIndex = -1;
        LoadGridview();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadGridview();
    }
    protected void XoaText()
    {
        txtmaloai.Text = "";
        txttenloai.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        XoaText();
    }
}
